<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class category extends Admin_Controller {

    var $theme_data = array(
        'layout_main' => 'layout',
        'left' => 'include/leftcp',
    );
	var $model = "mcategory";
	var $view = "category";
    function __construct() {
        parent::__construct();
        $this->load->model($this->model);
		$this->resCode = $this->{$this->model}->resCode;
        $this->url_index = admin_url() . $this->view;
        $this->folder_upload = "dataupload/".$this->view."/";
        $this->url_edit = $this->url_index . "/edit/";
        $this->url_delete = $this->url_index . "/delete/";
        $this->url_add = $this->url_index . "/edit/0";
    }

    public function index() {
		if (!$this->permission->check("select", $this->resCode))
			return $this->nopermission();
        $data = array();
        $data['datas'] = $this->{$this->model}->gets();
        $data['tresource'] = $this->mresource->get($this->resCode,'RES_CODE');
        $this->theme_data["main"] = $this->view."/index";
        $this->template->viewAdmin($this->theme_data, $data);
		
    }
	
    function edit($editID = 0) {
		if (!$this->permission->check("update", $this->resCode))
			return $this->nopermission();
			
        $data = array();
        $post = $this->input->post();
        $data['edit'] = $this->{$this->model}->get($editID, $this->{$this->model}->primaryKey);
		$data['tresource'] = $this->mresource->get($this->resCode,'RES_CODE');
        $data['error'] = array('err_code' => 0, 'msg' => '');
        if ($post) {
			if($upload = $this->upload(@$data['edit']->OS_THUMB)){
				$post['edit']['OS_THUMB'] = $this->folder_upload.$upload['upload_data']['file_name'];
			}
            if ($editID > 0) {
                $this->{$this->model}->update($editID, $this->{$this->model}->primaryKey, $post['edit']);
            }
			else{
				if (!$this->permission->check("insert", $this->resCode))
					return $this->nopermission();
				$editID = $this->{$this->model}->insert($post['edit']);
			}
            redirect($this->url_edit . $editID);
        }
		$data['editID'] = $editID;
		$data['categorys'] = $this->mcategory->gets();;
        $this->theme_data["main"] = $this->view."/edit";
        $this->template->viewAdmin($this->theme_data, $data);
    }
	
	function delete($deleteID = 0){
		if (!$this->permission->check("delete", $this->resCode))
			return $this->nopermission();
		if($deleteID > 0){
			$this->{$this->model}->delete($deleteID, $this->{$this->model}->primaryKey);
		}
	}
	
	function upload($old_name = '') {
		return ;
		$config['upload_path'] = './'.$this->folder_upload;
		$config['allowed_types'] = 'gif|jpg|png';
		
		if(!is_dir($this->folder_upload))
			mkdir($this->folder_upload,0777);
		$this->load->library('upload', $config);
		if ( ! $this->upload->do_upload())
		{
			return false;
		}
		else
		{
			if($old_name && isset($old_name))
			unlink('./'.$old_name);
			return array('upload_data' => $this->upload->data());
		}
		
	}
	
}
